﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace FuncWorks.Xna.UI.Menu
{
    public class MenuManager
    {
        #region Fields / Properties
		public MenuItemCollection MenuItems { get; private set; }
		public int SelectedIndex { get; set; }

		public TextStyle UnselectedStyle { get; set; }
		public TextStyle SelectedStyle { get; set; }
		public TextStyle DisabledStyle { get; set; }

        Rectangle targetRect;
        int Spacing;
        #endregion

        public MenuManager(Rectangle rectangle, TextStyle unselected, TextStyle selected, TextStyle disabled, int spacing)
        {
			SelectedIndex = 0;

            targetRect = rectangle;
            UnselectedStyle = unselected;
            SelectedStyle = selected;
            DisabledStyle = disabled;
            Spacing = spacing;
            MenuItems = new MenuItemCollection();
        }

        public void Activate()
        {
            MenuItems[SelectedIndex].Activate();
        }

        public void MoveLeft()
        {
            MenuItems[SelectedIndex].MoveLeft();
        }

        public void MoveRight()
        {
            MenuItems[SelectedIndex].MoveRight();
        }

        public void MoveUp()
        {
            int lastSelected = SelectedIndex;
            if (SelectedIndex > 0) { SelectedIndex--; }
            while (!MenuItems[SelectedIndex].Enabled || MenuItems[SelectedIndex].Hidden)
            {
                SelectedIndex--;
                if (SelectedIndex < 0)
                {
                    SelectedIndex = lastSelected;
                    break;
                }
            }
        }

        public void MoveDown()
        {
            int lastSelected = SelectedIndex;
            if (SelectedIndex < (MenuItems.Count - 1)) { SelectedIndex++; }
            while (!MenuItems[SelectedIndex].Enabled || MenuItems[SelectedIndex].Hidden)
            {
                SelectedIndex++;
                if (SelectedIndex == MenuItems.Count)
                {
                    SelectedIndex = lastSelected;
                    break;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            float totalHeight = 0;
            for (int i = 0; i < MenuItems.Count; i++)
            {
                IMenuItem item = MenuItems[i];
                if (item.Hidden) { item.Height = 0; continue; }
                TextStyle style = (i == SelectedIndex) ? SelectedStyle : (item.Enabled ? UnselectedStyle : DisabledStyle);
                totalHeight += item.CalculateHeight(style, i == SelectedIndex);
            }

            float currentY = targetRect.Top + (int)Math.Floor((targetRect.Height - totalHeight) / 2.0f);
            for (int i = 0; i < MenuItems.Count; i++)
            {
                IMenuItem item = MenuItems[i];
                if (item.Hidden) { continue; }
                TextStyle style = (i == SelectedIndex) ? SelectedStyle : (item.Enabled ? UnselectedStyle : DisabledStyle);
                item.Draw(spriteBatch, style, currentY, targetRect, i == SelectedIndex);
                currentY += item.Height + Spacing;
            }
        }
    }
}
